//
//  loginViewController.m
//  UI_13豆瓣作业
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "loginViewController.h"
#import "LTView.h"
#import "FourthViewController.h"
#import "regisViewController.h"
#import "DataBaseHandle.h"
#import "UserModel.h"

@interface loginViewController ()

@end

@implementation loginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"用户登录";

    [self createLoginUI];
        
    

}

- (void)createLoginUI {
    LTView *view1 = [[LTView alloc] initWithFrame:CGRectMake(20, 80, 350, 50)];
    view1.myLabel.text = @"用户名：";
    view1.myTextField.placeholder = @" 请输入用户名";
    view1.tag = 1000;
    [self.view addSubview:view1];
    
    LTView *view2 = [[LTView alloc] initWithFrame:CGRectMake(20, 150, 350, 50)];
    view2.myLabel.text = @"密码：";
    view2.myTextField.placeholder = @" 请输入密码";
    view2.myTextField.secureTextEntry = YES;
    view2.tag = 2000;
    [self.view addSubview:view2];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(20, 240, 150, 50);
    button1.backgroundColor = [UIColor greenColor];
    button1.layer.borderWidth = 1;
    button1.layer.borderColor = [UIColor lightGrayColor].CGColor;
    button1.layer.cornerRadius = 5;
    [button1 setTitle:@"登录" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(200, 240, 150, 50);
    button2.backgroundColor = [UIColor orangeColor];
    button2.layer.borderWidth = 1;
    button2.layer.borderColor = [UIColor lightGrayColor].CGColor;
    button2.layer.cornerRadius = 5;
    [button2 setTitle:@"注册" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(button2Action) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];

}



// 登录button方法***********************************************************************************************
- (void) buttonAction{
    LTView *view1 = [self.view viewWithTag:1000];
    LTView *view2 = [self.view viewWithTag:2000];
    // 用户名和密码都不为空
    if (![view1.myTextField.text isEqualToString:@""] && ![view2.myTextField.text isEqualToString:@""]) {
        // 从数据库中查询用户是否已经注册
        UserModel *model = [[DataBaseHandle sharedDataBaseHandle] selectUser:view1.myTextField.text];
        // 如果不存在，则提示注册
        if (model.userName == nil) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户不存在，请先注册" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
            // 如果存在，则去判断密码是否正确
        } else {
            if([model.password isEqualToString:view2.myTextField.text]) {
            // 密码正确则登录，并将用户的登录状态改成是
                // 先置为否，登陆后置为是
                model.state = @"否";
                [self.delegate changeItem];
                [[DataBaseHandle sharedDataBaseHandle] updateTable:model];
                [self.navigationController popViewControllerAnimated:YES];
                
            } else {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码错误" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alert animated:YES completion:^{
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
            }
        }
        
    } else if ([view1.myTextField.text isEqualToString:@""]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }  else if ([view2.myTextField.text isEqualToString:@""]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }

    
}

//***************************************************************************************************************

// 注册button方法
- (void) button2Action{
    regisViewController *regisVC = [[regisViewController alloc] init];
    [self.navigationController pushViewController:regisVC animated:YES];
    [regisVC release];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
